﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeTop.复习九月份
{
    public class Solution151
    {
        public string ReverseWords(string s)
        {
            int len = s.Length;
            //删除前导空格
            int index = 0;
            while (s[index] == ' ')
            {
                index++;
            }
            for (int i = index; i < len; i++)
            {




            }
            string curWord = "";
            Stack<string> stack = new Stack<string>();
            while (index < len)
            {
                while (index < len&&s[index] != ' ')
                {
                    curWord += s[index++];

                }
                //收集
                stack.Push(curWord);
                curWord = "";
                while (index < len&&s[index] == ' ')
                {
                    index++;
                }


            }

            string res = "";
            while (stack.Count > 1)
            {
                res += stack.Pop();
                res += " ";
            }
            res += stack.Pop();
            return res;

        }


    }
    class Program151
    {

        static void Main151(string[] args)
        {
            string s = "the sky is blue";
            Solution151 solution = new Solution151();
            string res = solution.ReverseWords(s);
            Console.WriteLine(res);
        }
    }
}
